package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.WorkOrderConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.HnWorkOrder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.HnWorkOrderActionBo;
import com.ruoyi.system.domain.vo.HnWorkOrderActionVo;
import com.ruoyi.system.domain.HnWorkOrderAction;
import com.ruoyi.system.mapper.HnWorkOrderActionMapper;
import com.ruoyi.system.service.IHnWorkOrderActionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工单处理记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@RequiredArgsConstructor
@Service
public class HnWorkOrderActionServiceImpl implements IHnWorkOrderActionService {

    private final HnWorkOrderActionMapper baseMapper;

    /**
     * 查询工单处理记录
     */
    @Override
    public HnWorkOrderActionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工单处理记录列表
     */
    @Override
    public TableDataInfo<HnWorkOrderActionVo> queryPageList(HnWorkOrderActionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HnWorkOrderAction> lqw = buildQueryWrapper(bo);
        Page<HnWorkOrderActionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工单处理记录列表
     */
    @Override
    public List<HnWorkOrderActionVo> queryList(HnWorkOrderActionBo bo) {
        LambdaQueryWrapper<HnWorkOrderAction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HnWorkOrderAction> buildQueryWrapper(HnWorkOrderActionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HnWorkOrderAction> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HnWorkOrderAction::getName, bo.getName());
        lqw.eq(bo.getDeptId() != null, HnWorkOrderAction::getDeptId, bo.getDeptId());
        lqw.eq(bo.getStatus() != null, HnWorkOrderAction::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), HnWorkOrderAction::getInfo, bo.getInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getOssIds()), HnWorkOrderAction::getOssIds, bo.getOssIds());
        lqw.eq(bo.getTroubleshootUserId() != null, HnWorkOrderAction::getTroubleshootUserId, bo.getTroubleshootUserId());
        lqw.eq(bo.getWorkId() != null, HnWorkOrderAction::getWorkId, bo.getWorkId());
        return lqw;
    }

    /**
     * 新增工单处理记录
     */
    @Override
    public Boolean insertByBo(HnWorkOrderActionBo bo) {
        HnWorkOrderAction add = BeanUtil.toBean(bo, HnWorkOrderAction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工单处理记录
     */
    @Override
    public Boolean updateByBo(HnWorkOrderActionBo bo) {
        HnWorkOrderAction update = BeanUtil.toBean(bo, HnWorkOrderAction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HnWorkOrderAction entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工单处理记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public HnWorkOrderAction createWorkOrderAction(HnWorkOrder icWorkOrder, Long userId) {
        HnWorkOrderAction workOrderAction = new HnWorkOrderAction();
        workOrderAction.setStatus(WorkOrderConstants.PROCESSING);
        workOrderAction.setName(icWorkOrder.getName());
        workOrderAction.setWorkId(icWorkOrder.getId());
        workOrderAction.setDeptId(icWorkOrder.getDeptId());
        workOrderAction.setInfo(icWorkOrder.getInfo());
        workOrderAction.setTroubleshootUserId(userId);
        workOrderAction.setOssIds(icWorkOrder.getOssIds());
        return workOrderAction;
    }
}
